package edu.mis.modules.category;

import com.jfinal.core.Controller;

import edu.mis.model.*;

public class CategoryController extends Controller{

	static CategoryService categoryService=new CategoryService();
	
	public void index() {
		render("category.html");
	}
	
	public void list() {
		setAttr("data",categoryService.findAllCategory());
		renderJson();
	}
	
	public void save() {
		Category category=getModel(Category.class);
		if(categoryService.save(category)){
			setAttr("result", true);
			setAttr("msg", "添加类别成功!");
		}else{
			setAttr("result",false);
		}
		renderJson(); 
	}
	
	public void edit() {
		//getPara获取参数为string类型，getParaToInt将获得的String转为int

		setAttr("category", categoryService.findCategoryById((getParaToInt("id"))));
		renderJson();
	}
	
	public void update(){
		Category category  =  getModel(Category.class);
		if(categoryService.update(category)){
			setAttr("result", true);
			setAttr("msg", "修改类别成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	
	public void delete() {
		if(categoryService.deleteCategoryById(getParaToInt("id"))){
			setAttr("result", true);
			setAttr("msg", "删除类别成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
}